package top.suven.base.http.admin.user.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import top.suven.base.common.excel.ColumnBox;
import top.suven.base.common.excel.ExportUtils;
import top.suven.base.core.mybatis.PageData;
import top.suven.base.http.admin.frame.config.ResponseResult;
import top.suven.base.http.admin.user.model.User;
import top.suven.base.http.admin.user.service.UserWebService;
import top.suven.base.http.admin.user.vo.request.UserRequestVo;
import top.suven.base.http.admin.user.vo.response.UserResponseVo;
import top.suven.base.http.admin.user.vo.valid.UserValidGroup;
import top.suven.base.http.enums.EnableStatusEnum;
import top.suven.base.http.enums.StatusEnum;
import top.suven.base.http.enums.SysMsgEnumType;
import top.suven.base.util.utilize.BeanUtil;
import top.suven.base.util.utilize.DateUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;



/**   
 * @Title: UserWebController.java
 * @Description: 用户的控制服务类
 * @author lixiangling
 * @date   2018/08/28 16:10:56
 * @version V1.0
 * ----------------------------------------------------------------------------
 *  modifyer    modifyTime                 comment
 *用户
 * ----------------------------------------------------------------------------
 *
 */
@Controller
@RequestMapping("/user/user")
public class UserWebController {
    private final static Logger logger = LoggerFactory.getLogger(UserWebController.class);
	@Autowired
	private UserWebService userWebService;
    /**
     * @Title: 跳转到用户主界面
     * @Description:userRequestVo @{Link UserRequestVo}
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/28 16:10:56
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @RequestMapping(value = "")
    @RequiresPermissions("user:user_list")
    public String index(){
        return "user/user_index";
    }
    /**
     * @Title: 获取用户分页信息
     * @Description:userRequestVo @{Link UserRequestVo}
     * @param
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/28 16:10:56
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @ResponseBody
    @RequestMapping(value = "list")
    @RequiresPermissions("user:user_list")
    public PageData<UserResponseVo> list(@Validated(value = {UserValidGroup.UserQueryGroup.class}) UserRequestVo userRequestVo){
        User user = new User();
        BeanUtil.copyProperties(userRequestVo, user);
        return userWebService.getUserPage(user, userRequestVo.getPage(), userRequestVo.getLimit());
    }
   /**
    * @Title: 新增用户信息
    * @Description:userRequestVo @{Link UserRequestVo}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/28 16:10:56
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @ResponseBody
    @RequestMapping(value = "add")
    @RequiresPermissions("user:user_add")
    public ResponseResult add(@Validated(value = {UserValidGroup.UserFormGroup.class}) UserRequestVo userRequestVo){
        User user = new User();
        BeanUtil.copyProperties(userRequestVo, user);
        user.setStatus(StatusEnum.VALID.getIndex());
        user.setEnableStatus(EnableStatusEnum.ENABLE.getIndex());
        userWebService.addUser(user);
        return ResponseResult.ok();
    }
    /**
    * @Title: 修改用户信息
    * @Description:userRequestVo @{Link UserRequestVo}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/28 16:10:56
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @ResponseBody
    @RequestMapping(value = "modify")
    @RequiresPermissions("user:user_modify")
    public ResponseResult modify(@Validated(value = {UserValidGroup.UserFormGroup.class}) UserRequestVo userRequestVo){
        User user = new User();
        BeanUtil.copyProperties(userRequestVo, user);
        if(user.getUserId() == null){
            return ResponseResult.error(SysMsgEnumType.SYS_WEB_UPD_INFO_NO_EXIST);
        }
        userWebService.updUser(user);
        return ResponseResult.ok();
    }
   /**
    * @Title: 查看用户信息
    * @Description:userRequestVo @{Link UserRequestVo}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/28 16:10:56
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @RequestMapping(value = "detail")
    @RequiresPermissions("user:user_list")
    public String detail(ModelMap modelMap, Long id){
        User user = null;
        if(id > 0){
            user = userWebService.getUser(id);
        }
        modelMap.put("user", user);
        return "user/user_detail";
    }
    /**
     * @Title: 跳转用户编辑界面
     * @Description:id @{Link Long}
     * @param
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/28 16:10:56
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @RequestMapping(value = "edit")
    @RequiresPermissions("user:user_modify")
    public String edit(ModelMap modelMap, @RequestParam(defaultValue = "0") Long id){
        User user = null;
        if(id > 0){
            user = userWebService.getUser(id);
        }
        modelMap.put("user", user);
        return "user/user_edit";
    }
    /**
    * @Title: 跳转用户新增编辑界面
    * @Description:id @{Link Long}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/28 16:10:56
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @RequestMapping(value = "newInfo")
    @RequiresPermissions("user:user_add")
    public String newInfo(ModelMap modelMap){
        return "user/user_edit";
    }
    /**
     * @Title: 删除用户信息
     * @Description:id @{Link Long}
     * @param
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/28 16:10:56
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @ResponseBody
    @RequestMapping("del")
    @RequiresPermissions("user:user_del")
    public ResponseResult del( @RequestParam(value = "id") Long[] ids){
        if (ids == null || ids.length == 0) {
            return ResponseResult.error(SysMsgEnumType.SYS_WEB_CHECK_INFO_FAIL);
        }
        userWebService.del(ids);
        return ResponseResult.ok();
    }
   /**
    * @Title: 启用用户信息
    * @Description:id @{Link Long}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/28 16:10:56
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @ResponseBody
    @RequestMapping(value = "turnOn")
    @RequiresPermissions("user:user_turnOn")
    public ResponseResult turnOn(@RequestParam(value = "id") Long[] ids){
        if (ids == null || ids.length == 0) {
            return ResponseResult.error(SysMsgEnumType.SYS_WEB_CHECK_INFO_FAIL);
        }
        userWebService.turnOn(ids);
        return ResponseResult.ok();
    }
   /**
    * @Title: 禁用用户信息
    * @Description:id @{Link Long}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/28 16:10:56
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @ResponseBody
    @RequestMapping(value = "turnOff")
    @RequiresPermissions("user:user_turnOff")
    public ResponseResult turnOff(@RequestParam(value = "id") Long[] ids){
        if (ids == null || ids.length == 0) {
            return ResponseResult.error(SysMsgEnumType.SYS_WEB_CHECK_INFO_FAIL);
        }
        userWebService.turnOff(ids);
        return ResponseResult.ok();
    }

    /**
    * @Title: 导出用户信息
    * @Description:id @{Link Long}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/28 16:10:56
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @RequestMapping("export")
    @RequiresPermissions("user:user_export")
    public void export(HttpServletResponse response, UserRequestVo userRequestVo){
        User user = new User();
        BeanUtil.copyProperties(userRequestVo, user);
        PageData<UserResponseVo> userPage = userWebService.getUserPage(user, userRequestVo.getPage(), userRequestVo.getLimit());
        List<UserResponseVo> data = userPage.getData();

        ColumnBox columnBox = ColumnBox.create()
                
			.add("userIdTmp", "会员ID即登录帐号")
			.add("userPwd", "登录密码")
			.add("userName", "会员名称")
			.add("userImageUrl", "会员头像")
			.add("userGender", "会员性别 数据字典")
			.add("userMobilePhone", "会员电话")
			.add("mobileCarriers", "会员电话所属运营商")
			.add("userEmail", "会员邮箱")
			.add("userLevel", "会员等级 数据字典")
			.add("userStatus", "会员状态")
			.add("bindPadMax", "绑定设备最大数")
			.add("resetValidataCode", "重置密码验证码")
			.add("resetValidataTime", "重置密码验证码有效期")
			.add("registerIp", "注册IP")
			.add("loginType", "登录方式 网页、客户端（android 、IOS、PC）")
			.add("loginIp", "登录IP")
			.add("loginTime", "登录时间")
			.add("loginCount", "登录次数")
			.add("creater", "创建人")
			.add("createTime", "创建时间")
			.add("modifier", "修改人")
			.add("modifyTime", "修改时间")
			.add("reorder", "排序")
			.add("remark", "备注")
			.add("enableStatus", "启用状态 启用-1、禁用-0")
			.add("userBirth", "出生日期")
			.add("cardId", "身份证号")
			.add("firstApplyStatus", "是否首次申请普通设备")
			.add("leftOnlineTime", "剩余在线时间（天）")
			.add("rbcAmount", "当前红豆币")
			.add("userEmailPwd", "邮箱登录密码")
			.add("edu", "学历")
			.add("occupation", "职业")
			.add("nickName", "昵称")
			.add("address", "地址")
			.add("qq", "QQ")
			.add("wechat", "微信")
			.add("mobileBindStatus", "手机号码绑定")
			.add("padLoginDailyStatus", "每日登录设备状态")
			.add("userId", "用户ID")
			.add("applyExperStatus", "是否首次申请vip设备体验")
			.add("inviteUserId", "邀请人ID（非邀请码注册为空）")
			.add("userSource", "客户端编号")
			.add("firstProbationalStatus", "是否首次申请体验设备")
			.add("scoreAmount", "用户积分")
			.add("scoreGrade", "积分等级")
			.add("mailboxVerificationCode", "")
			.add("emailExpireTime", "")
			.add("bindPadValidataCode", "")
			.add("bindPadExpireTime", "")
			.add("version", "")
			.add("boundPadCount", "绑定过的设备数")
			.add("client", "客户端类型，android、win、ios")
			.add("imei", "")
			.add("limitUse", "限制使用，1是、0否")
			.add("externalUserId", "客户端的user_id")
			.add("authType", "验证方式 1:现在支付身份验证 0:不验证  空表示未验证")
			.add("userClassify", "用户类型,1代表个人会员。2代表工作室会员")
			.add("openid", "第三方登录唯一标识")
			.add("openidType", "1:微信,2:qq,3:微博")
			.add("inviteType", "邀请类型1：任务邀请，2：活动邀请,3:七夕活动")
			.add("unionid", "微信unionid");

        try {
            ExportUtils.write(String.format("用户_%s.xls",DateUtil.date("yyyy-MM-dd",new Date().getTime())), response, data, columnBox);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}